<?php 
session_start();
if ($_SESSION['auth'] != true && $_SESSION['rule'] != "ADMIN") {
	die('Not log in');
}
include("includes/adminController.php");
$p = getPublisher($_GET['username'],null,null,null);
//$p = $publishers[0];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Admin</title>
<link href="itelement.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="script/jquery.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready (
	function () {
		$('#editPublisher').click(
			function(){
				var err = publisherReq();
				if(err != ''){
					$('#status').show('fast');
					$('#status').html(err);
					return;
				}
				//edit publisher
				var serialize = $('form').serializeArray( );
				$('#status').show('fast');
				$('#status').text('Wait server...');
				$.post("admin_operation.php?action=edit_publisher", serialize,
				  function(data){
					$('#status').text(data);
				  });
			}
		);
	}
);
function publisherReq(){
	var br = '<br />';
	var err = '';
	if($('#username').val()==''){
		err += 'o  Username field requires'+br;
	}
	/*if($('#password').val()==''){
		err += 'o  Password field requires'+br;
	}*/
	if($('#fname').val()==''){
		err += 'o  Firstname field requires'+br;
	}
	if($('#lname').val()==''){
		err += 'o  Lastname field requires'+br;
	}
	if($('#company').val()==''){
		err += 'o  Company field requires'+br;
	}
	if(err != '')err = err.substring(0,err.length-br.length);
	return err;
}
</script>
</head>
<body>
<form action="admin_operation.php?action=edit_publisher" method="post" name="form">
	  	  <div class="viewer-head">
		  	<div class="viewer-head-left">Edit Publisher</div>
			<!--<div class="viewer-head-right"><input type="button" value="reset"></div>-->
		  </div>
		  <div class="input">
			Username (can't change)
			  <div>
			  <input type="hidden" id="username" name="username" value="<? echo $p['username'];?>">			  
			  <input style="width:100%; color:#999999" type="text" value="<? echo $p['username'];?>">
			</div>
		  </div>
		  <div class="input">
			New Password (leave blank if don't edit password)
			  <div>
			  <input style="width:100%" id="password" name="password" type="password" value="">
			</div>
		  </div>
		  <div class="input">
			Firstname *
			  <div>
			  <input style="width:100%" id="fname" name="fname" type="text" value="<? echo $p['fname']?>">
			</div>
		  </div>
		  <div class="input">
			Lastname *
			  <div>
			  <input style="width:100%" id="lname" name="lname" type="text" value="<? echo $p['lname']?>">
			</div>
		  </div>
		  <div class="input">
			Company *
			  <div>
			  <input style="width:100%" id="company" name="company" type="text" value="<? echo $p['company']?>">
			</div>
		  </div>
		  <div class="input">
			Available Channel 
			  <div style="height:100px;overflow:auto">
			  	<table>
			<?php
			$all_channels = getAllChannel();
			$publisher_channels = searchPublisherChannel($p['username']);
			$publisher_channelid = array();
			if($publisher_channels != '')
			foreach($publisher_channels as $u){$publisher_channelid[]=$u['channelId'];}
			foreach($all_channels as $c)
			{
			if(in_array($c['id'],$publisher_channelid))
				echo "<tr><td><input type=\"checkbox\" id=\"chIdArray[]\" name=\"chIdArray[]\" value=\"".$c['id']."\" checked=\"checked\" /></td>";
			else
				echo "<tr><td><input type=\"checkbox\" id=\"chIdArray[]\" name=\"chIdArray[]\" value=\"".$c['id']."\"></td>";
			echo "<td class=\"list\">".$c['title']."</td></tr>";
			}
			?>
				</table>
			</div>
		  </div>
		  <div id="status"></div>
		  <input type="button" value="&nbsp;&nbsp;edit&nbsp;&nbsp;" id="editPublisher">
</form>
</body>
</html>